package the_eleven_chapter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MultiTalkClient {

	public static void main(String[] args) {
		try {
			// 向本机的2000端口发出客户端请求
			Socket socket = new Socket("127.0.0.1", 2000);
			// 由系统标准输入设备构造BufferedReader对象
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			//由Socket对象得到输出流，并构造PrintWriter对象
			PrintWriter os = new PrintWriter(socket.getOutputStream());
			//由Socket对象得到输入流，并构造相应的BufferedReader对象
			BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));

			String readline;
			//由系统标准读入一字符串
			readline = br.readLine();
			while (!readline.equals("bye")) {
				os.println(readline);
				//刷新输出流，使Server马上收到该字符串
				os.flush();
				System.out.println("Client: " + readline);
				System.out.println("Server: " + is.readLine());
				readline = br.readLine();
			}
			os.close();
			is.close();
			socket.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
